package com.limf.demo.collection;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.junit.jupiter.api.Test;

public class ListTest {

	@Test
	public void test13() {
		
		List<Integer> list = new ArrayList<>(10000000);
		for(int i=0;i<100000;i++) {
			list.add(i);
		}
		/**普通for循环**/
		long a = System.currentTimeMillis();
		
		int size = list.size();
		for(int i=0;i<size; i++) {
			System.out.println(list.get(i));
		}
		long b = System.currentTimeMillis();
		
		/**增强for循环**/
		long c = System.currentTimeMillis();
		for(Integer i: list) {
			System.out.println(i);
		}
		long d = System.currentTimeMillis();
		
		/**迭代器**/
		Iterator iterator = list.iterator();
		long e = System.currentTimeMillis();
		while(iterator.hasNext()) {
			System.out.println(iterator.next());
		}
		long f = System.currentTimeMillis();
		
		long g = System.currentTimeMillis();
		list.stream().forEach(System.out::println);
		long h = System.currentTimeMillis();
		
		System.out.println("普通for循环耗时："+(b-a)+"--ms");
		System.out.println("增强for循环耗时："+(d-c)+"--ms");
		System.out.println("迭代器耗时："+(f-e)+"--ms");
		System.out.println("java8-stream耗时："+(h-g)+"--ms");
		
	}
	
	//@Test
	public void test14() {
		
		List<Integer> list = new LinkedList<>();
		for(int i=0;i<100000;i++) {
			list.add(i);
		}
		/**普通for循环**/
		long a = System.currentTimeMillis();
		
		int size = list.size();
		for(int i=0;i<size; i++) {
			System.out.println(list.get(i));
		}
		long b = System.currentTimeMillis();
		
		/**增强for循环**/
		long c = System.currentTimeMillis();
		for(Integer i: list) {
			System.out.println(i);
		}
		long d = System.currentTimeMillis();
		
		/**迭代器**/
		Iterator iterator = list.iterator();
		long e = System.currentTimeMillis();
		while(iterator.hasNext()) {
			System.out.println(iterator.next());
		}
		long f = System.currentTimeMillis();
		
		long g = System.currentTimeMillis();
		list.stream().forEach(System.out::println);
		long h = System.currentTimeMillis();
		
		System.out.println("普通for循环耗时："+(b-a)+"--ms");
		System.out.println("增强for循环耗时："+(d-c)+"--ms");
		System.out.println("迭代器耗时："+(f-e)+"--ms");
		System.out.println("java8-stream耗时："+(h-g)+"--ms");
		
	}
	
}
